//
//  Layout.swift
//  weibo1
//
//  Created by changhongliang on 2017/9/6.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit

class Layout: NSObject {
    
   class func caculateHeight(status:Status) -> Double{
        var cellHeight = 0.0
        cellHeight = cellHeight + Double(cellmargin)
        cellHeight = cellHeight + Double(avatarHeight)
        cellHeight = cellHeight + Double(cellmargin)
        if let temp = status.text, temp != "" {
            let contentSize = (temp as NSString).boundingRect(with:CGSize(width: ScreenWidth - 20.0, height:  CGFloat(MAXFLOAT)) , options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 17.0)], context: nil)
//            HLLog("height\(contentSize)----text:\(temp)")
            cellHeight = cellHeight + Double(contentSize.height)
        }
        cellHeight = cellHeight + Double(cellmargin)
        //如果是转发微博
        if status.retweeted_status != nil{
            if let temp = status.retweeted_status?.text, temp != "" {
                let contentSize = (temp as NSString).boundingRect(with:CGSize(width: ScreenWidth - 20, height:  CGFloat(MAXFLOAT)) , options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 17.0)], context: nil)
                cellHeight = cellHeight + Double(contentSize.height)
                cellHeight = cellHeight + Double(cellmargin)
            }
            //计算图片的高度
            if status.retweeted_status != nil {
                cellHeight = cellHeight + Double(cellmargin)
                let contentViewSize = caculateSize(statusModel: status.retweeted_status!)
                cellHeight = cellHeight + Double(contentViewSize.height)
                cellHeight = cellHeight + Double(cellmargin)
            }
            
            
        }
        let oriContentViewSize = caculateSize(statusModel: status)
        if oriContentViewSize.height != 0 {
            cellHeight = cellHeight + Double(oriContentViewSize.height)
//            cellHeight = cellHeight + Double(cellmargin)
        }
        cellHeight = cellHeight + 54.0
        return cellHeight
    }
    
    
    
    //MARK:- 内部控制方法
    /// 计算配图的尺寸
    /// 第一个参数: imageView的尺寸
    /// 第二个参数: 配图容器的尺寸
    /*
     没有配图 :不显示CGSizeZero
     一张配图: 图片的尺寸就是配图和配图容器的尺寸
     四张配图: 田字格
     其他配图: 九宫格
     */
    
    class func caculateSize(statusModel:Status)->(CGSize)
    {
        // 1.获取配图个数
        let count = statusModel.pic_urls?.count ?? 0
        //2.1 判断有没有配图
        if count == 0 {
            return CGSize.zero
        }
        //2.2 判断是否是一张配图
        if count == 1 {
            //            let urlStr = statusViewModel?.thumbnail_pics!.last?.absoluteString
            //            let image = SDWebImageManager.shared().imageCache?.imageFromCache(forKey: urlStr ?? "")
            //            HLLog("1size:\(image!.size)")
            //            HLLog("2size:\(image!.size)")
            //            return (image!.size, image!.size)
            return CGSize(width: 90, height: 90)
        }
        
        let imageWidth:CGFloat = 90
        let imageHeight:CGFloat = imageWidth
        let imageMargin:CGFloat = 10
        
        //判断是否是4张配图
        if count == 4 {
            let col:CGFloat = 2
            //计算宽度 宽度 = 列数 *图片宽度 +(列数 - 1)*间隙
            let width = col * imageWidth + (col - 1) * imageMargin
            //计算高度
            let height = width
            return CGSize(width: width, height: height)
        }
        
        //其他配图 九宫格
        
        let col:CGFloat = 3
        let row = (count - 1 )/3 + 1
        let width = col * imageWidth + (col - 1) * imageMargin
        let height = CGFloat (row) * imageHeight + CGFloat(row - 1) * imageMargin
        return CGSize(width: width, height: height)
        
    }
    
 
}
